#include "CDrop.h"
#include "CPlayer.h"
#include "..\CCyberSpaceState.h"
#include "..\Wrappers\CSGD_TextureManager.h"
#include "..\Wrappers\CSGD_Direct3D.h"
#include "CCamera.h"
#include "..\Messaging\CSGP_EventSystem.h"

CDrop::CDrop(void)
{
	// Load the image IDs
	m_nPistolAmmoID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_PistolAmmo.png");
	m_nShotAmmoID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_ShotgunAmmo.png");
	m_nRocketAmmoID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_RocketAmmo.png");
	m_nHealthID	= CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_Health.png");
	m_nExGrenadeID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_ExGrenadePU.png");
	m_nSpeedID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_Speed.png");
	m_nFireRateID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/SGP_Gun.png");
	m_nMoneyID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/Money.png");

	CSGP_EventSystem::GetInstance()->RegisterClient("destroy.all.drops", this);

	CBase::SetType(OBJ_PICKUP);
}

CDrop::~CDrop(void)
{
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nSpeedID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nExGrenadeID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nHealthID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nRocketAmmoID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nShotAmmoID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nPistolAmmoID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nMoneyID);

	CSGP_EventSystem::GetInstance()->UnregisterClient("destroy.all.drops", this);
}

void CDrop::Update(float fElapsedTime)
{
	CBase::Update(fElapsedTime);

	m_fRotation += fElapsedTime;
}

void CDrop::Render(void)
{
	// Render the appropriate drop item
	if(GetDropType() == PISTOL)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nPistolAmmoID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(), 
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == SHOTGUN)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nShotAmmoID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(), 
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == ROCKET)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nRocketAmmoID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(), 
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == HEALTH)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nHealthID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(), 
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == FW_GRENADE)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nExGrenadeID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(), 
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == SPEED)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nSpeedID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(),
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == FIRE_RATE)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nFireRateID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(),
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}
	else if(GetDropType() == MONEY)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_nMoneyID, (int)GetPosX() - CCamera::GetInstance()->GetOffsetX(),
			(int)GetPosY() - CCamera::GetInstance()->GetOffsetY(), 1.0f, 1.0f);
	}

	// Show collison box
	//CSGD_Direct3D::GetInstance()->DrawRect(GetCollisionRect(), 0, 255, 0);
}

RECT CDrop::GetCollisionRect(void)
{
	RECT rIntersect;
	rIntersect.left = long(GetPosX());
	rIntersect.top = long(GetPosY());
	rIntersect.right = long(rIntersect.left + GetWidth());
	rIntersect.bottom = long(rIntersect.top + GetHeight());

	return rIntersect;
}

//bool CDrop::CheckCollision(IBaseInterface* pBase)
//{
//	CBase::CheckCollision(pBase);
//
//	//RECT rIntersect;
//
//	/*if(pBase->GetType() == CBase::OBJ_PLAYER) // If the player collides with the pickup
//	{
//		if(IntersectRect(&rIntersect, &pBase->GetRect(), &GetCollisionRect()))
//		{
//			if(GetDropType() == CDrop::PISTOL) // If it is pistol ammo
//			{
//				CPlayer::GetInstance()->SetPistolAmmo(CPlayer::GetInstance()->GetPistolAmmo()+10);
//			}
//		}
//	}*/
//
//	return false;
//}

void CDrop::HandleEvent(CEvent* pEvnt)
{
	if(pEvnt->GetEventID() == "destroy.all.drops")
	{
		CSGP_MessageSystem::GetInstance()->SendMessageA(new CDestroyDropMessage(this));
	}
}